# _*_coding:utf-8_*_
from . import InstCom
from ATS.hardwarescan import conf_file_manager


class INSTRUMENT:
    """
    Instrument base class
    類的組合
    """

    def __init__(self, name):
        """
        Initialize variable
        info_dict: Read instrument information from CONFIG file. Key--item name, Value--item value.
        limits_dict: Read corresponding model range limits from hwlimits.ini file.
        setting: Save instrument setting after sending command.
        name: Current instance name.
        """
        # info_list = conf_file_manager.get_info_dict(name)
        self.info_dict = conf_file_manager.get_info_dict(name)
        self.limits_dict = {}
        self.setting = {}
        self.name = name
        self.type = "INSTRUMENT"
        self.interface_type = self.getinterfacetype()  # LAN/GPIB/USB
        self.addr = self.getaddress()
        self.handler = self.__gethandler()

    def __gethandler(self):
        # return pyvisaImpl(self.info_dict).get_instrHandle()
        return InstCom.getinsthandler(self.info_dict)

    def getinfo(self, attr_str):
        """
        Get instrument information from info_dict.
        getattr(object, name[, default])
        object -- 对象。
        name -- 字符串，对象属性。
        default -- 默认返回值，如果不提供该参数，在没有对应属性时，将触发 AttributeError。
        """
        try:
            rvalue = getattr(self, "info_dict").get(attr_str.lower())
        except AttributeError:
            print("Instrument %s doesn't have the attribute --%s" % (self.name, attr_str.lower()))
            rvalue = None
        if isinstance(rvalue, str) and ("," in rvalue):
            rvalue = rvalue.strip(" ").strip(",")
            # rvalue = rvalue.strip(",")
            rvalue = [i.strip(" ") for i in rvalue.split(",")]
        return rvalue

    def getinterfacetype(self):
        return self.getinfo("interface_type")

    # @property
    def getaddress(self):
        interface_type = self.getinterfacetype()
        return self.getinfo(interface_type + "_addr")

    @property
    def getinsttype(self):
        """
        Get current instrument type
        """
        return self.type

    @property
    def getinstname(self):
        """
        Get name of current instrument.
        """
        return self.name

    def getinstsetting(self, key_list):
        setting = {}
        for k in key_list:
            setting[k] = self.setting.get(k)
        return setting

    def setinstsetting(self, setting_dict):
        for k in setting_dict:
            self.setting[k] = setting_dict[k]

    def clearinstsetting(self):
        self.setting = {}


class SCOPE(INSTRUMENT):
    def __init__(self, name):
        super().__init__(name)
        # INSTRUMENT.__init__(self, name, info_dict)


# SMU
class SMU(INSTRUMENT):
    def __init__(self, name):
        super().__init__(name)
        # INSTRUMENT.__init__(self, name, info_dict)


class CMTR(INSTRUMENT):
    def __init__(self, name):
        super().__init__(name)
        # self.cablelen = info_dict.get("cable_len", 0)

    def getcablelen(self):
        return self.cablelen


class MTRX(INSTRUMENT):
    def __init__(self, name):
        super().__init__(name)
        # INSTRUMENT.__init__(self, name, info_dict)
        # self.rowcount = info_dict.get("row_count", 0)
        # self.columncount = info_dict.get("column_count", 0)


class DIO(INSTRUMENT):
    def __init__(self, name):
        super(DIO, self).__init__(name)
        # super().__init__(name, info_dict)
        # INSTRUMENT.__init__(self, name, info_dict)


class VNA(INSTRUMENT):
    def __init__(self, name):
        super().__init__(name)
        # INSTRUMENT.__init__(self, name, info_dict)


class SA(INSTRUMENT):
    def __init__(self, name):
        super().__init__(name)
        # INSTRUMENT.__init__(self, name, info_dict)


class SS(INSTRUMENT):
    def __init__(self, name):
        super().__init__(name)
        # INSTRUMENT.__init__(self, name, info_dict)


class DCS(INSTRUMENT):
    def __init__(self, name):
        super().__init__(name)
        # INSTRUMENT.__init__(self, name, info_dict)
